/**
* 我的银行卡
*/
import React, { Component } from 'react';
import { View, StyleSheet, Text, Image, TouchableOpacity, FlatList, Alert, DeviceEventEmitter } from "react-native";
import { connect } from 'react-redux';
import { create_service } from '../../redux/index.js';
import { Contract } from '../../service/index.js';
import { Colors, Dimens, ImageRes } from '../../utils/Assets';
import { ProgressView } from '../custom/index'
import { Actions } from 'react-native-router-flux';
import { getStore } from '../../redux/index.js';
import Toast from 'react-native-root-toast';
class PersonalBankListView extends Component {

    constructor(props) {
        super(props);
        this.state = {
            loading: false,
            refreshing: false,
            delete: false,
            rightTitle: '管理',
            bank_list: []
        }
        this.pageSize = 10
        this.page = 1
        this.userInfo = getStore().getState().auth.userInfo
    }

    componentDidMount() {
        this.getDataList(true)
    }
    deleteClick() {
        if (this.selBank) {
            Alert.alert(
                '提醒',
                `确认删除  ${this.selBank.name}(${this.selBank.full_card_number.substring(this.selBank.full_card_number.length - 4)})`,
                [
                    { text: '取消', onPress: () => { } },
                    {
                        text: '确定', onPress: () => {
                            this.setState({ delete: !this.state.delete, rightTitle: '管理' }, () => {
                                this.deleteItem()
                            })
                        }
                    },
                ],
                { cancelable: false }
            )
        } else {
            Toast.show('请选要删除的银行卡', { position: Toast.positions.CENTER })
            return
        }
    }
    deleteItem() {
        this.setState({ loading: true })
        this.props.dispatch(create_service(Contract.POST_USER_BANKCARD_DELETE, { id: this.selBank.id, token: this.userInfo.token })).then((res) => {
            if (res) {
                this.page = 1
                this.getDataList(true)
            }
            this.setState({ loading: false })
        })
    }
    getDataList(refresh) {
        if (refresh) {
            this.setState({ refreshing: true })
        } else {
            this.setState({ loading: true })
        }
        this.props.dispatch(create_service(Contract.POST_USER_BANK_CARD_LIST, { page: this.page, pageSize: this.pageSize, type: '00', token: this.userInfo.token })).then((res) => {
            if (res) {
                if (refresh) {
                    this.setState({ bank_list: res.list, pagination: res.pagination, loading: false, refreshing: false })
                } else {
                    this.setState({ bank_list: this.state.bank_list.concat(res.list), pagination: res.pagination, loading: false, refreshing: false })
                }
            } else {
                this.setState({ loading: false, refreshing: false })
            }
        })
    }
    onEndReached() {
        if (this.state.bank_list.length != 0 && this.state.bank_list.length != this.state.pagination.total) {
            this.page += 1
            this.getDataList(false)
        }
    }
    componentWillUnmount() {
    }
    renderItem({ item, index }) {
        return (
            <View style={{ flexDirection: 'row', marginHorizontal: 15 }}>
                <View style={{ flex: 1, backgroundColor: item.color, padding: 15, marginTop: 15, borderRadius: 8 }}>
                    <View style={{ flexDirection: 'row' }}>
                        <Image style={{ width: 60, height: 60, borderRadius: 30 }} source={{ uri: item.bank_logo }}></Image>
                        <View style={{ justifyContent: 'center', marginLeft: 15 }}>
                            <Text style={{ fontSize: 16, color: "#ffffff" }}>{item.name}</Text>
                            <Text style={{ fontSize: 16, color: "#ffffff", marginTop: 15 }}>{item.type == '00' ? '储蓄卡' : '信用卡'}</Text>
                        </View>
                    </View>
                    <Text style={{ fontSize: 18, color: "#ffffff", marginTop: 40, marginLeft: 75, fontWeight: 'bold' }}>{"****  ****  ****  " + item.card_number}</Text>
                </View>
                {this.state.delete && <TouchableOpacity style={{ justifyContent: 'center', width: 30, height: 60, alignSelf: 'center' }} activeOpacity={0.8} onPress={() => {
                    for (let i = 0; i < this.state.bank_list.length; i++) {
                        let element = this.state.bank_list[i]
                        if (index == i) {
                            element.isSel = !element.isSel
                            if (element.isSel) {
                                this.selBank = element
                            } else {
                                this.selBank = null
                            }
                        } else {
                            element.isSel = false
                        }
                    }
                    this.setState({ loading: false })
                }}>
                    <Image style={{ width: 20, height: 20, marginLeft: 15, alignSelf: 'center' }} source={item.isSel ? ImageRes.personal_check : ImageRes.personal_uncheck}></Image>
                </TouchableOpacity>}
            </View>
        )
    }
    renderFootView() {
        return (
            <TouchableOpacity style={{ backgroundColor: '#ffffff', padding: 15, borderRadius: 8, width: Dimens.DeviceWidth - 30, flexDirection: 'row', marginLeft: 15, marginVertical: 20 }} onPress={() => {
                Actions.personalAddBankView({
                    reloadList: () => {
                        this.page = 1
                        this.getDataList(true)
                    }
                })
            }} activeOpacity={0.8} >
                <Image source={ImageRes.personal_plus} style={{ width: 18, height: 18, alignSelf: 'center' }}></Image>
                <Text style={{ fontSize: 16, color: "#333333", alignSelf: 'center', marginLeft: 15 }}>添加银行卡</Text>
            </TouchableOpacity>
        )
    }
    render() {
        return (
            <View style={styles.container}>
                <View style={styles.nav}>
                    <TouchableOpacity style={{ justifyContent: 'center', flex: 1 }} activeOpacity={0.8} onPress={() => {
                        Actions.pop()
                    }}>
                        <Image source={ImageRes.icon_back_white} style={{ width: 30, height: 30, marginLeft: 10 }}></Image>
                    </TouchableOpacity>
                    <View style={{ flex: 1, justifyContent: 'center' }}>
                        <Text style={{ color: '#ffffff', fontSize: 18, alignSelf: 'center', fontWeight: 'bold', alignSelf: 'center' }}>我的银行卡</Text>
                    </View>
                    <View style={{ flex: 1, justifyContent: 'flex-end', flexDirection: 'row' }}>
                        {this.state.rightTitle == '完成' && <TouchableOpacity style={{ justifyContent: 'center', marginRight: 15 }} onPress={() => {
                            this.deleteClick()
                        }}>
                            <Text style={{ fontSize: 16, color: '#FC2222', alignSelf: 'center' }}>{'删除'}</Text>
                        </TouchableOpacity>}
                        <TouchableOpacity style={{ justifyContent: 'center', marginRight: 15 }} onPress={() => {
                            this.setState({ rightTitle: this.state.rightTitle == '管理' ? '完成' : '管理', delete: !this.state.delete })
                        }}>
                            <Text style={{ fontSize: 16, color: '#ffffff', alignSelf: 'center' }}>{this.state.rightTitle}</Text>
                        </TouchableOpacity>
                    </View>
                </View>
                <View style={{ flex: 1 }}>
                    <FlatList
                        ref={(list) => (this.flatList = list)}
                        keyExtractor={(item, index) => index.toString()}
                        data={this.state.bank_list}
                        ItemSeparatorComponent={() => <View style={{ backgroundColor: Colors.bg_color, height: 1 }} />}
                        showsVerticalScrollIndicator={false}
                        renderItem={this.renderItem.bind(this)}
                        refreshing={this.state.refreshing}
                        onEndReachedThreshold={0.1}
                        ListFooterComponent={this.renderFootView.bind(this)}
                        onRefresh={() => {
                            this.page = 1
                            this.getDataList(true)
                        }}
                        onEndReached={() => {
                            this.onEndReached();
                        }}
                    />
                </View>
                <ProgressView show={this.state.loading} />
            </View>
        )
    }
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: Colors.bg_color,
    },
    nav: {
        height: Dimens.isAndroid ? 44 : (Dimens.isIPhoneX ? 88 : 64),
        backgroundColor: 'rgba(55, 142, 251, 1)',
        flexDirection: 'row',
        justifyContent: 'space-between',
        paddingTop: Dimens.isAndroid ? 0 : (Dimens.isIPhoneX ? 44 : 20)
    }
});
const ExportView = connect()(PersonalBankListView);
module.exports.PersonalBankListView = ExportView
